<template>
    <ElConfigProvider :locale="locale">
        <RouterView v-if="isRouterAlive" />
    </ElConfigProvider>
</template>

<script lang="ts" setup>
import { ref, nextTick, provide } from 'vue';
import { useRouter } from 'vue-router';
import { objectToFormData, getCookie } from '@/utils/utils';
import { baseLogUrl } from '@/api/api.request';
import { i18n, initLocaleSetting } from '@/i18n';
import { computed } from 'vue';
const router = useRouter();
let currentTime;

const isRouterAlive = ref(true);
const reload = () => {
    isRouterAlive.value = false;
    nextTick(() => {
        isRouterAlive.value = true;
    });
};
provide('reload', reload);

// 本地化词条
const locale = computed(() => {
    return i18n.global.getLocaleMessage(i18n.global.locale.value).element;
});
initLocaleSetting();

// 点击页签关闭按钮前端埋点
window.onbeforeunload = () => {
    const currentRouteData = router.currentRoute.value;
    currentTime = Date.now();
    const token = getCookie('SaaS_Token') ? getCookie('SaaS_Token') : '';
    const actData = {
        model: 'saas-housekeeper-web-worker',
        page: currentRouteData.path.replace(/\//g, '_').slice(1),
        ts: currentTime,
        token: token,
    };
    const actions = [
        {
            item: 'close',
            descriptions: '用户点击页签关闭按钮',
        },
    ];
    const actData2 = objectToFormData(actData);
    actData2.append('actions', JSON.stringify(actions));
    navigator.sendBeacon(baseLogUrl + 'log/logger', actData2);
};
</script>
